package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Format;

public class ChargeInfo  {
	
	public static final String dbName = "UserDB";
	public static final String collection = "ChargeInfo";
	
	private ObjectId _id;
	
	//用户id
	private String user_id;
	//收费总额
	private Double payment;
	//收费时间
	private String paydate;
	//有效期开始时间
	private String startdate;
	//有效期结束时间
	private String enddate;
	//收费项
	private String item;
	//操作员id
	private String operator_id;
	
	@JsonIgnore
	public String get_id() {
		return _id.toString();
	}

	public void set_id(ObjectId _id) {
		this._id = _id;
	}
	@JsonIgnore
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public Double getPayment() {
		return payment;
	}
	public void setPayment(Double payment) {
		this.payment = payment;
	}
	public String getPaydate() {
		return paydate;
	}
	public void setPaydate(String paydate) {
		this.paydate = paydate;
	}
	public String getStartdate() {
		return startdate;
	}
	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}
	public String getEnddate() {
		return enddate;
	}
	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	
	@JsonIgnore
	public String getOperator_id() {
		return operator_id;
	}
	public void setOperator_id(String operator_id) {
		this.operator_id = operator_id;
	}
	
	public String getUsername() {
		
		User user = new User();
		user = user.getUserByUser_id(this.user_id);
		return user.getUsercode();
	}
	
	public String getOperator() {
		
		User user = new User();
		user = user.getUserByUser_id(this.operator_id);
		return user.getUsercode();
	}
		
	@Override
	public String toString() {
		return "ChargeInfo [user_id=" + user_id + ", payment=" + payment
				+ ", paydate=" + paydate + ", startdate=" + startdate
				+ ", enddate=" + enddate + ", item=" + item + ", operator_id="
				+ operator_id + "]";
	}
	public ChargeInfo(String user_id, Double payment, String paydate,
			String startdate, String enddate, String item, String operator_id) {
		super();
		this.user_id = user_id;
		this.payment = payment;
		this.paydate = paydate;
		this.startdate = startdate;
		this.enddate = enddate;
		this.item = item;
		this.operator_id = operator_id;
	}
	
	
	public ChargeInfo() {
		// TODO Auto-generated constructor stub
	}
	
	public ChargeInfo(Document doc) {
		
		_id = doc.getObjectId("_id");
		user_id = doc.getString("user_id");
		payment = doc.getDouble("payment");
		paydate = doc.getString("paydate");
		startdate = doc.getString("startdate");
		enddate = doc.getString("enddate");
		item = doc.getString("item");
		operator_id = doc.getString("operator_id");
		
	}
	
	/**
	 * 用于测试时向数据库中写入数据
	 */
	public void test(){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		List<String> users = new ArrayList<>();
		users.add("user");
		users.add("user_pay");
		
		User admin = new User();
		admin = admin.getUserByUserCode("admin");
				
		//users.add("admin");
		for(int j = 0; j < users.size(); j++){
			
			User user = new User();
			user = user.getUserByUserCode(users.get(j));
			String user_id = user.get_id();
			ChargeInfo info = new ChargeInfo(user_id, 34.5, Format.date2String(new Date(), 3), Format.date2String(new Date(), 3), Format.date2String(new Date(), 3), "付费用户", admin.get_id());
			Document t = info.toDocument();
			db.insert(t);
		}
		
		
		db.closeConnection();
		
	}

	public Document toDocument() {
		// TODO Auto-generated method stub
		return new Document("user_id", user_id).append("payment", payment)
				.append("paydate", paydate).append("startdate", startdate)
				.append("enddate", enddate).append("item", item)
				.append("operator_id", operator_id);
	}
	
	/**
	 * 获取 ChargeInfo 表中数据条数
	 * @return
	 */
	@JsonIgnore
	public long getCount(){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		long count = db.collectionGetCount();
		db.closeConnection();
		return count;
		
	}
	
	public static void main(String[] args) {
		
		ChargeInfo info = new ChargeInfo();
		info.test();
	}
	
}
